package com.wuxiaofan.product.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuxiaofan.product.entity.Category;
import com.wuxiaofan.product.entity.custom.CategoryCustom;
import com.wuxiaofan.product.service.CategoryService;
import com.wuxiaofan.common.vo.ReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.swagger.annotations.Api;

@RestController
@Api("商品三级分类")
@RequestMapping("/category")
@Slf4j
public class CategoryApi {


    @Autowired
    private CategoryService pmsCategoryService;

    @GetMapping("list/tree")
    public ReturnResult tree(){
        CategoryCustom categoryCustom = pmsCategoryService.tree();
        return ReturnResult.success("查询分类集合",categoryCustom);
    }

    @GetMapping("/findById/{id}")
    public ReturnResult findById(@PathVariable("id") String id){
        Category pmsCategory = pmsCategoryService.findById(id);
        return ReturnResult.success("",pmsCategory);
    }

    @RequestMapping("findByMap")
    public ReturnResult findByMap(@RequestBody JSONObject jsonObject){
        Map paraMap = new HashMap();
        paraMap.putAll(jsonObject);
        List<CategoryCustom> pmsCategoryCustoms = pmsCategoryService.findByMap(paraMap);
        return ReturnResult.success("",pmsCategoryCustoms);
    }

    @RequestMapping("all")
    public ReturnResult all(){
        Map paraMap = new HashMap();
        List<CategoryCustom> pmsCategoryCustoms = pmsCategoryService.findByMap(paraMap);
        return ReturnResult.success("",pmsCategoryCustoms);
    }

    @RequestMapping("findByPage")
    public ReturnResult findByPage(@RequestBody JSONObject jsonObject){
        Page page = new Page(jsonObject.getIntValue("current"),jsonObject.getIntValue("size"));
        Map paraMap = new HashMap();
        paraMap.putAll(jsonObject);
        IPage<CategoryCustom> pmsCategoryCustoms = pmsCategoryService.findByMap(paraMap,page);
        return ReturnResult.success("",pmsCategoryCustoms);
    }

    @RequestMapping("save")
    public ReturnResult save(@RequestBody Category pmsCategory){
        pmsCategory = pmsCategoryService.save(pmsCategory);
        return ReturnResult.success("保存成功",pmsCategory);
    }

    @RequestMapping("update")
    public ReturnResult update(@RequestBody JSONObject jsonObject){
        Map paraMap = jsonObject;
        pmsCategoryService.updateMapById(paraMap);
        return ReturnResult.success("更新成功");
    }


    @RequestMapping("delByIds")
    public ReturnResult delByIds(@RequestBody JSONArray jsonArray){
        log.info("jsonArray:{}",jsonArray);
        List<Long> ids = jsonArray.toJavaList(Long.class);
        pmsCategoryService.delByIds(ids);
        return ReturnResult.success("删除成功");
    }


}
